// RPC stubs for clients to talk to lock_server

#include "lock_client.h"
#include "rpc.h"
#include <arpa/inet.h>

#include <sstream>
#include <iostream>
#include <stdio.h>

lock_client::lock_client(std::string dst)
{
	sockaddr_in dstsock;
	make_sockaddr(dst.c_str(), &dstsock);		//dst.c_str() : port number

	//jul
	//printf("dst %s\n", dst.c_str());

	cl = new rpcc(dstsock);
	if (cl->bind() < 0) {
		printf("lock_client: call bind\n");
	}
}

int
lock_client::stat(lock_protocol::lockid_t lid)
{
	int r;
	lock_protocol::status ret = cl->call(lock_protocol::stat, cl->id(), lid, r);
	VERIFY (ret == lock_protocol::OK);

	//jul
	printf("client stat id %d, lid %ld, r %d\n", cl->id(), lid, r);	//client stat id 2033971898, lid 1, r 0

	return r;
}

lock_protocol::status
lock_client::acquire(lock_protocol::lockid_t lid)
{
	printf("%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__);
	int r;
	lock_protocol::status ret = cl->call(lock_protocol::acquire, cl->id(), lid, r);
	return ret;
}

lock_protocol::status
lock_client::release(lock_protocol::lockid_t lid)
{
	printf("%s, %s, %d\n", __FILE__, __FUNCTION__, __LINE__);
	int r;
	lock_protocol::status ret = cl->call(lock_protocol::release, cl->id(), lid, r);
	return ret;
}

